/**
 * 账号权限
 */
//通过缓存获取用户信息
var item2 = localStorage.getItem("item");
var userInfoObj = JSON.parse(item2);
//遮照信号量
var viewflag = true;
var userNumber;
mini.parse();
//得到元素
var grid = mini.get("datagrid1");						//当前登陆人信息表
var treegridModualr = mini.get('treegridModualr');		//显示登陆人可看用户表
var addBtn = mini.get('addBtn');						//新增按钮
var removeBtn = mini.get('removeBtn');					//删除按钮
//全局变量
var url = bootPATH + "../frame/frame1/pages/";      //路径前缀
var nowUserName;									//当前登陆人的账号名
var nowUName;										//当前登陆人的人员名
var nowUserArr = [];								//当前登陆人信息list
var nowUserObj = {};								//当前登陆人信息对象
var statuss = false;								//勾选是true;(默认是false)
//设置
mini.get("sp").set({
	height : document.body.clientHeight - 40
});
//var menuColumns = new ColumnsMenu(grid);
//页面初始化
$(function (){
	//grid.load(ajaxUrl + '/user_jurisdiction/findsame.do');
	grid.load(ajaxUrl + '/user/list.do');
	grid.load();
	// nowUserName = userInfoObj.userName;
	// nowUserObj = {
	// 		id: userInfoObj.userId,
	// 		userjurisdiction: userInfoObj.userName,
	// 		username: userInfoObj.uName
	// };
	// nowUserArr.push(nowUserObj);
	// //当前登陆用户表格赋值
	// grid.setData(nowUserArr);
})

//数据表选择(选择用户得到模块)
function onSelectionChanged(e) {
	//按钮禁用取消(新增/删除)
	addBtn.enable(); 
	removeBtn.enable();
	//得到被选择的id
	nowUserName = e.selected.userName;
	nowUName = e.selected.uName;
	//请求该用户下可看账户的列表数据
	treegridModualr.load(ajaxUrl + '/user_jurisdiction/findusername.do?userNumber=' + nowUserName );

	treegridModualr.load();
	if (viewflag) {
		treegridModualr
			.mask({
				html : "<div></div>",
				cls : "mini-panel",
				style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
			});
	}
}
//新增用户
function add(){
	var row = grid.getSelecteds();
	console.log(row);
	var userNumber=row[0].userName;
	console.log(userNumber);
	//弹出用户参照列表
	mini.open({
        url: url +"alertBox/userNumWindow.html",
        title: "新增", width: 600, height: 400,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = { action: "new", data: {nowUserName:nowUserName,nowUName:nowUName,userNumber:userNumber}};
            iframe.contentWindow.SetData(data);
        },
        ondestroy: function (action) {
        	//请求该用户下可看账户的列表数据
        	treegridModualr.load(ajaxUrl + "?userName=" + nowUserName);
        }
    });
}
//删除用户
function remove(){

	var gridRow = grid.getSelected();
	var treeRow = treegridModualr.getSelected();
	if(gridRow){
		if(treeRow){
			var nowUserNumber = gridRow.userName;
			var nowUserName = treeRow.userName;
			mini.confirm("确定删除选中记录？","系统提示",function(action){
				if(action == "ok"){
					$.ajax({
						url: ajaxUrl + "/user_jurisdiction/deleteqx.do",
						type:"post",
						data: {
							userName: nowUserName,
							userNumber: nowUserNumber
						},
						success: function (data) {
							if(data.state == 'error'){
								grid.unmask();
								mini.alert(data.message);
							}else{
								//请求该用户下可看账户的列表数据
								// treegridModualr.load(ajaxUrl + "?userName=" + nowUserName);
								// treegridModualr.load();


								treegridModualr.load(ajaxUrl + '/user_jurisdiction/findusername.do?userNumber=' + nowUserName );

								treegridModualr.load();
							}
						},
						error: function () {
							mini.alert('失败');
						}
					});
				}
			});
		}else{
			mini.alert('请选择可查看人员');
		}
	}else{
		mini.alert('请选择需要删除的账号');
	}

}
//过滤
function onFilterChanged(e) {
	var rcValue = mini.get("rcFilter").getValue().toLowerCase();
	var rnValue = mini.get("rnFilter").getValue().toLowerCase();
	var rdValue = mini.get("rdFilter").getValue().toLowerCase();
	grid.filter(function(row) {
		var r1 = true;
		if (rcValue) {
			r1 = String(row.roleCode).toLowerCase().indexOf(rcValue) != -1;
		}
		var r2 = true;
		if (rnValue) {
			r2 = String(row.roleCode).toLowerCase().indexOf(rnValue) != -1;
		}
		var r3 = true;
		if (rdValue) {
			r3 = String(row.roleCode).toLowerCase().indexOf(rdValue) != -1;
		}
		return r1 && r2 && r3;
	});
}
//遮照
function resize() {
	if (viewflag) {
		treegridModualr.mask({
			html : "<div></div>",
			cls : "mini-panel",
			style : "width: 100%; height: 100%;filter:alpha(Opacity=80);-moz-opacity:0.5;opacity: 0.5;"
		});
	}
}
//数组去重
function uniq(array){
	var temp = [];
	for(var i = 0; i < array.length; i++) {
		//如果当前数组的第i项在当前数组中第一次出现的位置是i，才存入数组；否则代表是重复的
		if(array.indexOf(array[i]) == i){
			temp.push(array[i])
		}
	}
	return temp;
}
